import { createRouter, createWebHashHistory } from 'vue-router'

const router = createRouter({
  history: createWebHashHistory(), // hash模式：createWebHashHistory，history模式：createWebHistory
  routes: [
    {
      path: '/',
      redirect: '/introduce'
    },
    {
      path: '/introduce',
      name: 'introduce',
      component: () => import( '../views/Introduce.vue')
    },
    {
      path: '/dashboard',
      name: 'dashboard',
      component: () => import( '../views/Index.vue')
    },
    {
      path: '/login',
      name: 'login',
      component: () => import('../views/Login.vue')
    },
    {
      path: '/add',
      name: 'add',
      component: () => import( '../views/AddGood.vue')
    },

    {
      path: '/new',
      name: 'new',
      component: () => import( '../views/IndexConfig.vue')
    },
    {
      path: '/stu',
      name: 'stu',
      component: () => import( '../views/Stu.vue')
    },
    {
      path: '/guest',
      name: 'guest',
      component: () => import( '../views/Guest.vue')
    },
    {
      path: '/teacher',
      name: 'teacher',
      component: () => import( '../views/Teacher.vue')
    },
    {
      path: '/account',
      name: 'account',
      component: () => import(/* webpackChunkName: "account" */ '../views/Account.vue')
    }
  ]
})
//路由跳转页面之前
router.beforeEach((to, from ,next) => {
  const whiteList = ["/login", "/reg"] ;//白名单地址
  //sessionStorage,localStorage
  //从本地会话存储中获取登录凭证token
  const token = sessionStorage.getItem("token");
  //如果有凭证或者在请求白名单中，就正常往后面跳转页面
  if(token || whiteList.includes(to.path)){
    next(); //征程跳转到目标地址
  }
  else{
    next("/login");//未登陆，跳转到登录页
  }
})

export default router
